package com.zhx.threadcoreknowledge.threadobjectclasscommonmethod;

/**
 * @author ：zhanghaixuan
 * @date ：Created in 2019-10-03 11:23
 * 用途         ： 展示wait和notify的基本用法
 * 1. 研究代码的执行顺序
 * 2. 证明wait是释放锁的
 */
public class Wait {

    private static Object object = new Object();

    static class Thread1 extends Thread{
        @Override
        public void run() {
           synchronized (object){
               System.out.println("线程1开始运行");
               try {
                   object.wait();
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               System.out.println("线程1被唤醒并执行完毕了");
           }
        }
    }

    static class Thread2 extends Thread{
        @Override
        public void run() {
            synchronized (object){
                System.out.println("线程2开始运行");
                object.notify();
                System.out.println("线程2执行完毕了");
            }
        }
    }

    public static void main(String[] args) {
        Thread1 thread1 = new Thread1();
        Thread2 thread2 = new Thread2();

        thread1.start();
        try {
            Thread.sleep(30);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread2.start();

    }
}
